package org.dromara.Nyt7BusinessEntity.controller;

import java.util.List;

import cn.dev33.satoken.config.SaTokenConfig;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.ibatis.annotations.Param;
import org.dromara.Nyt7BusinessEntity.domain.Nyt7BusinessEntity;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;

import org.dromara.Nyt7BusinessEntity.domain.vo.Nyt7BusinessEntityVo;
import org.dromara.Nyt7BusinessEntity.domain.bo.Nyt7BusinessEntityBo;
import org.dromara.Nyt7BusinessEntity.service.INyt7BusinessEntityService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 经营主体
 *
 * @author Lion Li
 * @date 2025-01-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/Nyt7BusinessEntity/Nyt7BusinessEntity")
public class Nyt7BusinessEntityController extends BaseController {

    private final INyt7BusinessEntityService nyt7BusinessEntityService;
    private final SaTokenConfig getSaTokenConfig;

    /**
     * 查询经营主体列表
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:list")
    @GetMapping("/list")
    public TableDataInfo<Nyt7BusinessEntityVo> list(Nyt7BusinessEntityBo bo, PageQuery pageQuery) {
        return nyt7BusinessEntityService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出经营主体列表
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:export")
    @Log(title = "经营主体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Nyt7BusinessEntityBo bo, HttpServletResponse response) {
        List<Nyt7BusinessEntityVo> list = nyt7BusinessEntityService.queryList(bo);
        ExcelUtil.exportExcel(list, "经营主体", Nyt7BusinessEntityVo.class, response);
    }

    /**
     * 获取经营主体详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:query")
    @GetMapping("/{id}")
    public R<Nyt7BusinessEntityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(nyt7BusinessEntityService.queryById(id));
    }

    /**
     * 新增经营主体
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:add")
    @Log(title = "经营主体", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Nyt7BusinessEntityBo bo) {
        return toAjax(nyt7BusinessEntityService.insertByBo(bo));
    }

    /**
     * 修改经营主体
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:edit")
    @Log(title = "经营主体", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Nyt7BusinessEntityBo bo) {
        return toAjax(nyt7BusinessEntityService.updateByBo(bo));
    }

    /**
     * 删除经营主体
     *
     * @param ids 主键串
     */
    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:remove")
    @Log(title = "经营主体", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(nyt7BusinessEntityService.deleteWithValidByIds(List.of(ids), true));
    }


   // PUT /Nyt7BusinessEntity/Nyt7BusinessEntity/cancelCertification/{id}
   @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:edit")
   @Log(title = "经营主体", businessType = BusinessType.UPDATE)
   @RepeatSubmit()
    @PutMapping("/cancelCertification/{id}")
    public R<Void> removerCertification(@Validated(EditGroup.class)  @PathVariable Long id) {
       return toAjax(nyt7BusinessEntityService.removeCertification(id));
   }

    @SaCheckPermission("Nyt7BusinessEntity:Nyt7BusinessEntity:add")
    @Log(title = "经营主体", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Boolean> excelImport(MultipartFile file) throws Exception {
        // 使用静态方法导入Excel文件
        List<Nyt7BusinessEntity> list = ExcelUtil.importExcel(file.getInputStream(), Nyt7BusinessEntity.class);

        // 处理导入的数据（保存到数据库）

        return R.ok( nyt7BusinessEntityService.saveBatch(list));
    }


}


